# Replication file for "After the Ballot Box: How Explicit Racist Appeals Damage Constituents Views of Their Representation in Government
# Authors: Maneesh Arora and Christopher T. Stout
# Journal: Research & Politics 

# Load packages
library(tidyverse)
library(hrbrthemes)
library(ggsci)
library(data.table)
library(readstata13)
library(Hmisc)
library(effects)

# Load data
data <- read.dta13("combinedlucids032020.dta")

# Subset the data by race and ideology

black <- data[ which(data$black==1 & data$smithtreatment2 != 1), ]
white <- data[ which(data$white==1), ]
whitecons <- white[ which(white$ideol>0.5), ]
whitelib <- white[ which(white$ideol<0.5), ]
whitemod <- white[ which(white$ideol==0.5), ]

# T-tests for Table 1 
t.test(interestssmith ~ smithtreatment1,
       data = black)
t.test(interestssmith ~ smithtreatment1, 
       data = white)
t.test(interestssmith ~ smithtreatment1, 
       data = whitelib)
t.test(interestssmith ~ smithtreatment1, 
       data = whitemod)
t.test(interestssmith ~ smithtreatment1, 
       data = whitecons)

# Regression models 
brep <- lm(interestssmith ~ smithtreatment1 + party + college +
             racialresentment + female + age, data = black)
summary(brep)

wrepcons <- lm(interestssmith ~ smithtreatment1 + party + college +
                 racialresentment + female + age, data = whitecons)
summary(wrepcons)

wreplib <- lm(interestssmith ~ smithtreatment1 + party + college +
                racialresentment + female + age, data = whitelib)
summary(wreplib)

wrepmod <- lm(interestssmith ~ smithtreatment1 + party + college +
                racialresentment + female + age, data = whitemod)
summary(wrepmod)

# Figure 1

#save the model 
eff_cf <- effect("smithtreatment1", brep) #predicted probabilities
eff_cf2 <- effect("smithtreatment1", wreplib) #predicted probabilities
eff_cf3 <- effect("smithtreatment1", wrepmod) #predicted probabilities
eff_cf4 <- effect("smithtreatment1", wrepcons) #predicted probabilities

# save everything as a data frame
eff_cf <- data.frame(eff_cf)
eff_cf <- eff_cf %>% mutate(model="Black")
eff_cf2 <- data.frame(eff_cf2)
eff_cf2 <- eff_cf2 %>% mutate(model="White Liberal")
eff_cf3 <- data.frame(eff_cf3)
eff_cf3 <- eff_cf3 %>% mutate(model="White Moderate")
eff_cf4 <- data.frame(eff_cf4)
eff_cf4 <- eff_cf4 %>% mutate(model="White Conservative")
eff_df <- bind_rows(eff_cf, eff_cf2, eff_cf3, eff_cf4)

# Build the plot 
eff_df %>% 
  ggplot(aes(x=smithtreatment1, y=fit, color=model, linetype=model, fill=model)) +
  geom_ribbon(aes(ymin=lower, ymax=upper), alpha=.25, color="transparent", show.legend = FALSE) +
  geom_line() +
  labs(x="Treatment", y="Interest Representation", color="") +
  guides(linetype = FALSE) +
  scale_x_continuous(breaks = c(0,1)) + 
  theme_classic() +
  theme(legend.position = "bottom")

# ggsave("hydesmith3.pdf")